package com.cian.controller;


import com.cian.entity.Subsystem;
import com.cian.entity.dto.SubsystemForStockDTO;
import com.cian.entity.dto.WebLinkDTO;
import com.cian.service.impl.AccountServiceImpl;
import com.cian.service.impl.SubsystemServiceImpl;
import com.cian.utils.IdenCreateByUUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Cian
 * @since 2021-03-04
 */
@RestController
@RequestMapping("//subsystem")
public class SubsystemController {
    @Autowired
    AccountServiceImpl accountService;
    @Autowired
    SubsystemServiceImpl subsystemService;

    @RequestMapping("/link")
    public String linkAccept(@RequestParam("account") String account,
                             @RequestParam("licenseCode") String licenseCode,
                             @RequestParam("longitude") double longitude,
                             @RequestParam("latitude")double latitude,
                             @RequestParam("detail") String detail){
//        System.out.println(account + " " + licenseCode);
        Integer id = accountService.selectByNameAndLicense(account, licenseCode);
//        System.out.println(id);
        // 排除账户错误的情况
        if(id == null) return null;
        Subsystem subsystem = new Subsystem();
        subsystem.setSLatitude(latitude);
        subsystem.setSLongitude(longitude);
        subsystem.setSDetail(detail);
        subsystem.setServerAccount(account);
        return subsystemService.createSubSystem(subsystem, new IdenCreateByUUID());
    }

    @RequestMapping("/obtain")
    public List<WebLinkDTO> obtainSubsystem(@RequestParam("serve-account") String serveAccount){
        return subsystemService.selectByServeAccount(serveAccount);
    }

    @RequestMapping("/obtain/stock")
    public List<SubsystemForStockDTO> obtainWebForStock(@RequestParam("serve-account") String serveAccount){
        return subsystemService.selectByServeForStock(serveAccount);
    }

}

